#pragma once

#include <iostream>
#include <string>
#include "TcpSever.hpp"

using namespace TcpServerModule;


const std::string Sep = "\r\n";
const std::string BlankLine = Sep;


class HttpServer
{
public:
    HttpServer(int port):
        _tsvr(std::make_unique<TcpServer>(port))
    {

    }
    void Start()
    {
        _tsvr->InitServer([this](SockPtr sockfd,InetAddr client){
            return this->HandlerHttpRequest(sockfd,client);
        });
        _tsvr->Loop();
    }
    bool HandlerHttpRequest(SockPtr sockfd,InetAddr client)
    {
        LOG(LogLevel::DEBUG) << "HttpServer: get a new client: " << sockfd->Fd() 
        << " addr info: " << client.Addr();
        std::string http_request;
        //目前我们读取到的是最原始的信息
        sockfd->Recv(&http_request);
        std::cout << http_request << std::endl;
        //读取请求,进行文本处理
        //1.读取请求的完整性  --  暂时不做了
        //2.完整http反序列化,http response序列化...
        std::string status_line = "HTTP/1.1 200 OK" + Sep + BlankLine;
        std::string body = "<!DOCTYPE html>\
                            <html>\
                            <head>\
                            <meta charset = \"UTF-8\">\
                            <title> Hello World</title>\
                            </head>\
                            <body>\
                            <p> Hello World</p>\
                            </body> </html>";
        std::string httpresponse = status_line + body;
        sockfd->Send(httpresponse);

        return true;
    }
    ~HttpServer()
    {

    }
private:
    std::unique_ptr<TcpServer> _tsvr;
};


